package com.potenza.training.dictionary.controller;

import com.potenza.training.dictionary.entity.HopePosition;
import com.potenza.training.dictionary.server.HopePositionService;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/hope")
public class HopePositionController {

    @Autowired
    private HopePositionService hopePositionService;

    @Autowired
    private RedisTemplate redisTemplate;

    static List<HopePosition> hopePositionList = new ArrayList<HopePosition>();

    /**
     * 获取所有的期望职位
     * @return
     */
    @GetMapping("/position")
    public List<HopePosition> findAll(){
        return hopePositionService.findAll();
    }

    /**
     * 根据Pid查询期望职位
     * @return
     */
    @GetMapping("/position/grouping/{pid}")
    public List<HopePosition> findByPid(@PathVariable Integer pid){
        return hopePositionService.findByPid(pid);
    }

    /**
     * 根据id查询期望职位
     * @return
     */
    @GetMapping("/position/grouping")
    public HopePosition findById(@RequestParam Integer id){
        return hopePositionService.findById(id);
    }

    /**
     * 保存
     */
    @PostMapping("/position/save")
    @RequiresPermissions("hope:position:save")
    public R save(@RequestBody HopePosition hopePosition){
        hopePositionService.saveHopePosition(hopePosition);

        return R.ok();
    }
    /**
     * 修改
     */
    @PostMapping("/position/update")
    @RequiresPermissions("hope:position:update")
    public R update(@RequestBody HopePosition hopePosition){
        hopePositionService.updateHopePosition(hopePosition);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/position/delete")
    @RequiresPermissions("hope:position:delete")
    public R delete(@RequestBody Integer[] ids){
        redisTemplate.delete("hopePosition");
        hopePositionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取经过处理的联动数据
     * @return
     */
    @GetMapping("/positions")
    public List<HopePosition> edit(){
        hopePositionList.clear();
        List<HopePosition> all = findAll();
        for (HopePosition hopePosition : all) {
            hopePositionList.add(new HopePosition(hopePosition.getId(),hopePosition.getName(),hopePosition.getPid(),null));
        }

        List<HopePosition> collect = hopePositionList.stream().filter(hopePosition ->
                hopePosition.getPid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrens(menu, hopePositionList));
            return menu;
        }).collect(Collectors.toList());

        return collect;
    }

    //递归查找所有菜单的子类菜单
    private static List<HopePosition> getChildrens(HopePosition root,List<HopePosition> all){
        List<HopePosition> children = all.stream().filter(linkageEntities -> Long.parseLong(String.valueOf(linkageEntities.getPid()))==root.getId()).map(linkageEntities -> {
            //1、递归找到子菜单
            linkageEntities.setChildren(getChildrens(linkageEntities,all));
            return linkageEntities;
        }).collect(Collectors.toList());
        return children;
    }
}
